package dialog;

import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

import com.browniebytes.javafx.control.DateTimePicker;
import com.lanhai.model.Job;

import application.Main;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
/**
 * 任务列表
 * @author liangmin
 *
 */
public class JobListDialog {

//	private static final Logger logger = Logger.getLogger(JobListDialog.class);
	@FXML
	private Button search;
	@FXML
	private DateTimePicker beginTime;
	@FXML
	private DateTimePicker endTime;
	@FXML
	private TableView<Job> job_list;
	@FXML
	private TextField agvId;
	@FXML
	private TextField line;	
	public ObservableList<Job> jobData;
	
	public void init(Main main) {
		setListField();
    	search.setOnAction((ActionEvent t) -> {
    		String bstr = beginTime.dateTimeProperty().get().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    		String estr = endTime.dateTimeProperty().get().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    		String agvNo = agvId.getText();
    		String lineNo = line.getText();
    		Map<String, Object> params = new HashMap<String, Object>();
    		params.put("type", "job");
    		params.put("beginTime", bstr);
    		params.put("endTime", estr);
    		params.put("path", "search");
    		params.put("noPopup", "true");
    		params.put("agvId", agvNo);
    		params.put("line", lineNo);
    		JSONObject jo = main.httpRequest(params);
    		if(jo.getBoolean("success")){
    			JSONArray data = jo.getJSONArray("data");
        		if(data.size() > 0){
        			jobData = FXCollections.observableArrayList();
            		for(int i=0; i<data.size(); i++){
            			JSONObject obj = data.getJSONObject(i);
            			Job job = new Job();
            			job.setId(obj.getInt("id"));
            			job.setAgvId(obj.getInt("agvId"));
            			job.setCallTime(obj.getString("callTime"));
            			job.setFinishTime(obj.getString("finishTime"));
            			job.setLine(obj.getString("mwLine"));
            			jobData.add(job);           			
            		}
            		job_list.setItems(jobData);   			
        		}else{
        			job_list.getItems().clear();
        		}
    		}
    	});
	}
	
	public void setListField(){
		TableColumn<Job, Integer> col1 = new TableColumn<>("序号");
		col1.setCellValueFactory(new PropertyValueFactory<>("id"));
		col1.setPrefWidth(60);
		col1.setStyle("-fx-alignment:CENTER;");
		TableColumn<Job, String> col2 = new TableColumn<>("线别");
		col2.setCellValueFactory(new PropertyValueFactory<>("line"));
		col2.setPrefWidth(60);
		col2.setStyle("-fx-alignment:CENTER;");
		TableColumn<Job, Integer> col3 = new TableColumn<>("AGV编号");
		col3.setCellValueFactory(new PropertyValueFactory<>("agvId"));
		col3.setPrefWidth(100);
		col3.setStyle("-fx-alignment:CENTER;");
		TableColumn<Job, String> col4 = new TableColumn<>("呼叫时间");
		col4.setCellValueFactory(new PropertyValueFactory<>("callTime"));
		col4.setPrefWidth(150);
		col4.setStyle("-fx-alignment:CENTER;");
		TableColumn<Job, String> col5 = new TableColumn<>("完成时间");
		col5.setCellValueFactory(new PropertyValueFactory<>("finishTime"));
		col5.setPrefWidth(150);
		col5.setStyle("-fx-alignment:CENTER;");	
		job_list.getColumns().add(col1);
		job_list.getColumns().add(col2);
		job_list.getColumns().add(col3);
		job_list.getColumns().add(col4);
		job_list.getColumns().add(col5);
	}
	
}
